package com.slingshot.identitylibrary.data

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONObject
import com.slingshot.identitylibrary.api.CommonApi
import io.reactivex.Observable
import io.reactivex.subjects.PublishSubject
import okhttp3.Call
import okhttp3.Callback
import okhttp3.OkHttpClient
import java.io.IOException
import java.nio.charset.StandardCharsets

internal class Repository {

    fun extractIdCardFace(appId: String, appSecret: String, imageBase64: String): Observable<String> {
        val subject = PublishSubject.create<String>()
        val client = OkHttpClient()
        val request = CommonApi.extractIdCardFace(appId, appSecret, imageBase64).build()
        val mCall = client.newCall(request)
        try {
            val callback: Callback = object : Callback {
                override fun onFailure(call: Call, e: IOException) {
                    subject.onError(java.lang.Exception(e.message))
                }

                override fun onResponse(call: Call, response: okhttp3.Response) {
                    val result = backgroundParser(response)
                    subject.onNext("$result")
                    subject.onComplete()
                }
            }
            mCall.enqueue(callback)
        } catch (e: IllegalStateException) {
            subject.onError(java.lang.Exception(e.message))
        } catch (e: Exception) {
            subject.onError(java.lang.Exception(e.message))
        }
        return subject.doOnDispose { mCall.cancel() }
    }

    fun extractIdCardNationalEmblem(appId: String, appSecret: String, imageBase64: String): Observable<String> {
        val subject = PublishSubject.create<String>()
        val client = OkHttpClient()
        val request = CommonApi.extractIdCardNationalEmblem(appId, appSecret, imageBase64).build()
        val mCall = client.newCall(request)
        try {
            val callback: Callback = object : Callback {
                override fun onFailure(call: Call, e: IOException) {
                    subject.onError(java.lang.Exception(e.message))
                }

                override fun onResponse(call: Call, response: okhttp3.Response) {
                    val result = backgroundParser(response)
                    subject.onNext("$result")
                    subject.onComplete()
                }
            }
            mCall.enqueue(callback)
        } catch (e: IllegalStateException) {
            subject.onError(java.lang.Exception(e.message))
        } catch (e: Exception) {
            subject.onError(java.lang.Exception(e.message))
        }
        return subject.doOnDispose { mCall.cancel() }
    }

    fun faceDetect(appId: String, appSecret: String, imageBase64: String,imageBase64_id: String): Observable<String> {
        val subject = PublishSubject.create<String>()
        val client = OkHttpClient()
        val request = CommonApi.faceDetect(appId, appSecret, imageBase64,imageBase64_id).build()
        val mCall = client.newCall(request)
        try {
            val callback: Callback = object : Callback {
                override fun onFailure(call: Call, e: IOException) {
                    subject.onError(java.lang.Exception(e.message))
                }

                override fun onResponse(call: Call, response: okhttp3.Response) {
                    val result = backgroundParser(response)
                    subject.onNext("$result")
                    subject.onComplete()
                }
            }
            mCall.enqueue(callback)
        } catch (e: IllegalStateException) {
            subject.onError(java.lang.Exception(e.message))
        } catch (e: Exception) {
            subject.onError(java.lang.Exception(e.message))
        }
        return subject.doOnDispose { mCall.cancel() }
    }
    fun faceDetect(appId: String, appSecret: String, imageBase64: String): Observable<String> {
        val subject = PublishSubject.create<String>()
        val client = OkHttpClient()
        val request = CommonApi.faceDetect(appId, appSecret, imageBase64).build()
        val mCall = client.newCall(request)
        try {
            val callback: Callback = object : Callback {
                override fun onFailure(call: Call, e: IOException) {
                    subject.onError(java.lang.Exception(e.message))
                }

                override fun onResponse(call: Call, response: okhttp3.Response) {
                    val result = backgroundParser(response)
                    subject.onNext("$result")
                    subject.onComplete()
                }
            }
            mCall.enqueue(callback)
        } catch (e: IllegalStateException) {
            subject.onError(java.lang.Exception(e.message))
        } catch (e: Exception) {
            subject.onError(java.lang.Exception(e.message))
        }
        return subject.doOnDispose { mCall.cancel() }
    }

    @Throws(java.lang.Exception::class)
    private fun backgroundParser(response: okhttp3.Response): JSONObject {
        assert(response.body() != null)
        val body = response.body()!!.bytes()
        val bodyString: String = byteArrayToString(body)
        return JSON.parseObject(bodyString)
    }

    private fun byteArrayToString(bytes: ByteArray): String {
        return String(bytes, StandardCharsets.UTF_8)
    }
}